<?php

/**
 * 版本管理
 *
 * @author suning <snsnsky@gmail.com>
 */

use \MobileApi\AppInfo;
use \Common\Bridge;

define('SITE_AUTH', 'mobile-app-version');
require('../../../common.inc.php');

class version extends \lib\core\AdminAction
{
    /**
     * app_id 集合
     */
    private $appinfo = [];

    /**
     * 表名
     */
    const TABLE = 'AppVersion';

    /**
     * 构造函数
     */
    public function __construct()
    {
        global $app;
        parent::__construct($app);

        $this->appinfo = loadconf('mobileapi/base');
    }

    /**
     * 首页
     */
    public function doDefault()
    {
        $smarty = $this->app->page();

        $page = max(1, intval($_GET['page']));
        $app_id = intval($_GET['app_id']);
        $app_id = isset($this->appinfo[$app_id]) ? $app_id : MOBILE_APPID_IPHONE;

        $where = ['AppId' => $app_id];

        $pdo_r = Bridge::pdo(Bridge::DB_MOBILE_R);
        $total = $pdo_r->clear()
            ->select('count(Id)')
            ->from(self::TABLE)
            ->where($where)
            ->getValue();

        if ($total > 0)
        {
            $data = $pdo_r->clear()
                ->select('*')
                ->from(self::TABLE)
                ->where($where)
                ->order('Id DESC')
                ->limit(($page - 1) * self::PAGE_SIZE, self::PAGE_SIZE)
                ->getAll();
            $pagination = $this->pagination($total, self::PAGE_SIZE, $page);

            $smarty->value('data', $data);
            $smarty->value('pagination', $pagination);
        }

        $smarty->value('app_ids', $this->appinfo);
        $smarty->value('app_id', $app_id);
        $smarty->output('mobile/version.tpl');
    }

    /**
     * 添加入口
     */
    public function doAdd()
    {
        $app_id = intval($_POST['app_id']);
        $version_code = trim($_POST['version_code']);
        $intro = trim($_POST['intro']);
        $down_url = trim($_POST['down_url']);

        if (!isset($this->appinfo[$app_id]))
            $this->output(['status' => false, 'msg' => '请选择应用！']);
        if (empty($version_code))
            $this->output(['status' => false, 'msg' => '应用版本号不能为空！']);
        if (empty($intro))
            $this->output(['status' => false, 'msg' => '应用版本升级描述不能为空！']);
        if (empty($down_url))
            $this->output(['status' => false, 'msg' => '应用升级下载链接地址不能为空！']);
        if (!filter_var($down_url, FILTER_VALIDATE_URL))
            $this->output(['status' => false, 'msg' => '请填写有效的应用升级下载链接地址！']);

        $data = [
            'AppId' => $app_id,
            'VersionCode' => $version_code,
            'Intro' => $intro,
            'DownUrl' => $down_url,
            'CreateTime' => date('Y-m-d H:i:s')
        ];
        $pdo_w = Bridge::pdo(Bridge::DB_MOBILE_W);

        if ($pdo_w->insert(self::TABLE, $data))
        {
            AppInfo::removeCache(AppInfo::CACHE_VERSION, $app_id);
            $this->adminLog('添加手机应用版本',  $pdo_w->lastInsertId(), 'add', serialize($data));
            $this->output(array('status' => true, 'msg' => '添加成功！'));
        } else {
            $this->output(array('status' => false, 'msg' => '操作失败， 请稍后再试！'));
        }
    }

    /**
     * 删除入口
     */
    public function doDelete()
    {
        $id = intval($_GET['id']);
        $app_id = intval($_GET['app_id']);
        if (!$id || !isset($this->appinfo[$app_id])) {
            HDshowMsg('删除失败',['type'=>'error','desc'=>'参数非法！']);
        }

        $pdo_w = Bridge::pdo(Bridge::DB_MOBILE_W);
        $where = ['Id' => $id, 'AppId' => $app_id];
        if($pdo_w->delete(self::TABLE, $where))
        {
            AppInfo::removeCache(AppInfo::CACHE_VERSION, $app_id);
            $this->adminLog('删除手机应用版本', $id, 'del', serialize($where));
            HDshowMsg('删除成功！');
        }
        else
            HDshowMsg('删除失败',['type'=>'error','desc'=>'请稍后再试！']);
    }
}

$app->run();
